package com.jarvan.controller;

import com.jarvan.enums.ResultEnum;
import com.jarvan.exception.TrainException;
import com.jarvan.form.PassengerForm;
import com.jarvan.model.Passenger;
import com.jarvan.service.impl.PassengerServiceImpl;
import com.jarvan.vo.ResultVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(value = "旅客api")
@Slf4j
@RestController
@RequestMapping("/passenger")
public class PassengerController {

    @Autowired
    private PassengerServiceImpl passengerService;

    @PostMapping("/signup")
    public ResultVo<Passenger> create(@Valid PassengerForm passengerForm,
                                      BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("[用户注册]  参数错误  passengerForm={}", passengerForm);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(),
                    ResultEnum.PARAM_ERROR.getMessage());
        }
        Passenger passenger = passengerService.findByName(passengerForm.getLoginName());
        if (passenger != null){
            log.error("[用户注册]  用户名已存在！loginName={}", passengerForm.getLoginName());
            throw new TrainException(ResultEnum.PASSENGER_ALREADY_EXIST.getCode(),
                    ResultEnum.PASSENGER_ALREADY_EXIST.getMessage());
        }
        return null;
    }
}
